<?php namespace App\Http\Controllers;

use App\ActivityBargain;
use App\ActivityBargainInitiate;
use App\ActivityBargainJoin;
use App\ActivityGroup;
use App\ActivityGroupAdd;
use App\ActivityGroupPrice;
use App\Admin;
use App\AdvertImg;
use App\Banner;
use App\BannerNav;
use App\BooksChapter;
use App\BooksInfo;
use App\Classify;
use App\Comment;
use App\CommentImg;
use App\Coupons;
use App\Express;
use App\Goods;
use App\GoodsBanner;
use App\GoodsImg;
use App\GoodSpu;
use App\Http\Controllers\Api\AudioController;
use App\Http\Controllers\Api\GameController;
use App\Http\Services\FileManager;
use App\OrderAddress;
use App\QrCodes;
use App\ShowGoods;
use App\UserIntegralRecord;
use App\UserScore;
use App\UserScoreRecord;
use App\UserTotoalScore;
use DB;
use App\OrderItem;
use App\Orders;
use App\RechargePackage;
use App\User;
use App\UserAddress;
use App\UserInfo;
use App\UserSpendRecord;
use Illuminate\Contracts\Auth\Guard;
use Auth;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function bannerList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $sysBanners = Banner::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.banner-list',['banners'=>$sysBanners]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId($id)->first();

        return view('admin.banner-set',['banner'=>$thatBanner]);
    }



    public function advertImgList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $sysAdvertImg =AdvertImg::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.advert-img-list',['advertImgs'=>$sysAdvertImg]);
    }

    public function advertImgSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatAdvertImg = AdvertImg::whereId($id)->first();

        $showGoods = ShowGoods::whereId(1)->first();

        return view('admin.advert-img-set',['advertImg'=>$thatAdvertImg,'showGoods' => $showGoods]);
    }

    public function userList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $users = User::orderBy("created_at","desc")->paginate(50);

        foreach($users as $user){
            /** @var User $user */
            $user->userInfo;
        }

        return view('admin.user-list',["users"=>$users]);
    }

    public function adminList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Admin $newAdmin */
        $newAdmins = Admin::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.admin-list', ['admins' => $newAdmins]);
    }

    public function adminSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.admin-set');
    }

    public function classifyList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify = Classify::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.classify-list',['classify' => $classify]);
    }

    public function goodsList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Goods $good */
        $goods = Goods::whereType('common')->orderBy('created_at','desc')->paginate(30);
        if(count($goods) > 0){
            foreach($goods as $good){
                $good->thumb_url = FileManager::getImgUrl($good->thumb_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($good->classify_id)->first();
                if($thisClassify){
                    $good->classify_name = $thisClassify->name;
                }else{
                    $good->classify_name = '-';
                }
            }
        }

        return view('admin.goods-list',['goods' => $goods]);
    }

    public function goodsSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $sysGoods = Goods::whereId($id)->first();
        if($sysGoods){
            $sysGoods->thumb_url = FileManager::getImgUrl($sysGoods->thumb_id);
        }

        $classifys = Classify::get();

        $showGoods = ShowGoods::whereId(1)->first();

        return view('admin.goods-set',['sysGoods' => $sysGoods,'classifys' => $classifys,'showGoods' => $showGoods]);
    }

    public function goodsIntegralList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Goods $good */
        $goods = Goods::whereType('integral')->orderBy('created_at','desc')->paginate(30);
        if(count($goods) > 0){
            foreach($goods as $good){
                $good->thumb_url = FileManager::getImgUrl($good->thumb_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($good->classify_id)->first();
                if($thisClassify){
                    $good->classify_name = $thisClassify->name;
                }else{
                    $good->classify_name = '-';
                }
            }
        }

        return view('admin.goods-integral-list',['goods' => $goods]);
    }


    public function goodsIntegralSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $sysGoods = Goods::whereId($id)->first();
        if($sysGoods){
            $sysGoods->thumb_url = FileManager::getImgUrl($sysGoods->thumb_id);
        }

        $classifys = Classify::get();

        return view('admin.goods-integral-set',['sysGoods' => $sysGoods,'classifys' => $classifys]);
    }


    public function goodsBannerList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $goodsId = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var GoodsBanner $goodsBanner */
        $goodsBanners = GoodsBanner::whereGoodId($goodsId)->get();
        if(count($goodsBanners) > 0){
            foreach($goodsBanners as $goodsBanner){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($goodsBanner->good_id)->first();
                if($thisGoods){
                    $goodsBanner->goods_name = $thisGoods->name;
                }else{
                    $goodsBanner->goods_name = '-';
                }

                $goodsBanner->img_url = FileManager::getImgUrl($goodsBanner->img_id);
            }
        }

        return view('admin.goods-banner-list',['goodsBanners' => $goodsBanners,'goodsId' => $goodsId]);
    }

    public function goodsImgList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $goodsId = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var GoodsImg $goodsImg */
        $goodsImgs = GoodsImg::whereGoodId($goodsId)->get();
        if(count($goodsImgs) > 0){
            foreach($goodsImgs as $goodsImg){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($goodsImg->good_id)->first();
                if($thisGoods){
                    $goodsImg->goods_name = $thisGoods->name;
                }else{
                    $goodsImg->goods_name = '-';
                }

                $goodsImg->thumb_url = FileManager::getImgUrl($goodsImg->thumb_id);
            }
        }

        return view('admin.goods-img-list',['goodsImgs' => $goodsImgs,'goodsId' => $goodsId]);
    }

    public function ordersList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $orderNum  = $request->get('order_num');
        $startTime = $request->get('start_time');
        $endTime   = $request->get('end_time');

        $orders = DB::table('orders')->whereNull('deleted_at');

        if(!is_null($orderNum) && $orderNum != ''){
            $orders->whereOrderNum($orderNum);
        }

        if((!is_null($startTime) && $startTime != '') && (!is_null($endTime) && $endTime != '')){
            $orders = $orders->whereBetween('created_at',[$startTime,$endTime]);
        }

        $orders = $orders->orderBy('created_at','desc')->paginate(30);
        if(count($orders) > 0){
            foreach($orders as $order){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($order->user_id)->first();
                if($thisUserInfo){
                    $order->nickname  = $thisUserInfo->nickname;
                    $order->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                }else{
                    $order->nickname  = null;
                    $order->photo_url = null;
                }

                /** @var OrderAddress $userAddress */
                $userAddress = OrderAddress::whereId($order->address_id)->first();
                if($userAddress){
                    $order->Address  = $userAddress->province.$userAddress->city.$userAddress->area.$userAddress->address;
                    $order->name     = $userAddress->consignee;
                    $order->phone    = $userAddress->phone;
                    $order->postcode = $userAddress->postcode;
                }else{
                    $order->Address = null;
                }
            }
        }

        /** @var Express $thisExpress */
        $thisExpress = Express::get();

        return view('admin.orders-list',['orders' => $orders,'orderNum' => $orderNum,'startTime' => $startTime,'endTime' => $endTime,'thisExpress' => $thisExpress]);
    }

    public function ordersDetails(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;
        /** @var Orders $thisOrder */
        /** @var Orders $order */
        $order = Orders::whereId($id)->first();
        if($order){
            /** @var UserInfo $thisUserInfo */
            $thisUserInfo = UserInfo::whereUserId($order->user_id)->first();
            if($thisUserInfo){
                $order->nickname  = $thisUserInfo->nickname;
                $order->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
            }else{
                $order->nickname  = null;
                $order->photo_url = null;
            }

            /** @var Express $thisExpress */
            $thisExpress = Express::whereId($order->express)->first();
            if($thisExpress){
                $order->express_name = $thisExpress->name;
            }else{
                $order->express_name = null;
            }

            /** @var OrderAddress $userAddress */
            $userAddress = OrderAddress::whereId($order->address_id)->first();
            if($userAddress){
                $order->address = $userAddress->province.$userAddress->city.$userAddress->area.$userAddress->address;
            }else {
                $order->address = null;
            }

        }

        /** @var OrderItem $orderItem */
        $orderItems = OrderItem::whereOrderId($id)->get();
        if(count($orderItems) > 0){
            foreach($orderItems as $orderItem){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($orderItem->good_id)->first();
                if($thisGoods){
                    $orderItem->goods_name = $thisGoods->name;
                    /** @var Classify $classify */
                    $classify = Classify::whereId($thisGoods->classify_id)->first();
                    if($classify){
                        $orderItem->classify_name = $classify->name;
                    }else{
                        $orderItem->classify_name = null;
                    }
                }else{
                    $orderItem->goods_name = null;
                    $orderItem->classify_name = null;
                }
            }
        }

        return view('admin.orders-details',['thisOrder' => $order,'orderItems' => $orderItems]);
    }

    public function writeShipInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $orderId = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Orders $thisOrder */
        $thisOrder = Orders::whereId($orderId)->first();
        if($thisOrder){
            /** @var UserAddress $userAddress */
            $userAddress = OrderAddress::whereId($thisOrder->address_id)->first();
            if($userAddress){
                $thisOrder->Address  = $userAddress->province.$userAddress->city.$userAddress->area.$userAddress->add_detail;
                $thisOrder->name     = $userAddress->consignee;
                $thisOrder->phone    = $userAddress->phone;
                $thisOrder->postcode = $userAddress->postcode;
            }else{
                $thisOrder->Address = null;
            }
        }

        /** @var Express $thisExpress */
        $thisExpress = Express::get();

        return view('admin.write-ship-info',['thisOrder' => $thisOrder,'thisExpress' => $thisExpress]);
    }

    public function goodsCommentList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $goodsId = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Comment $comment */
        $comments = Comment::whereGoodsId($goodsId)->orderBy('created_at','desc')->paginate(30);
        if(count($comments) > 0){
            foreach($comments as $comment){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($comment->user_id)->first();

                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($comment->goods_id)->first();

                $comment->goods_name = $thisGoods->name;
                $comment->nickname = $thisUserInfo->nickname;
                $comment->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
            }
        }

        return view('admin.goods-comment-list',['comments' => $comments,'goodsId' => $goodsId]);
    }

    public function goodsCommentImgList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $commentId = !is_null($request->get('id')) ? $request->get('id') :0;

        $goodsId = $request->get('goods_id');

        /** @var CommentImg $commentImg */
        $commentImgs = CommentImg::whereCommentId($commentId)->get();
        if(count($commentImgs) > 0){
            foreach($commentImgs as $commentImg){
                $commentImg->img_url = FileManager::getImgUrl($commentImg->img_id);
            }
        }

        return view('admin.goods-comment-img-list',['commentImgs' => $commentImgs,'goodsId' => $goodsId]);
    }

    public function couponsList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $coupons = Coupons::orderBy('created_at','desc')->paginate(20);

        if(count($coupons)>0){
            foreach($coupons as $coupon){

                $coupon->img_url=FileManager::getImgUrl($coupon->img_id);
            }
        }



        return view('admin.coupons-list',['coupons' => $coupons]);
    }

    public function couponsSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisCoupon = Coupons::whereId($id)->first();

        return view('admin.coupons-set',['thisCoupon' => $thisCoupon]);
    }

    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.change-psw');
    }

    public function bannerNavList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $sysBanners = BannerNav::orderBy('created_at','desc')->get();

        return view('admin.banner-nav-list',['banners'=>$sysBanners]);
    }
    public function bannerNavSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = BannerNav::whereId($id)->first();

        return view('admin.banner-nav-set',['banner'=>$thatBanner]);
    }

    public function goodsSpuList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $goodsId = !is_null($request->get('goods_id')) ? $request->get('goods_id') : 0;
        $type = 'goods';

        /** @var GoodSpu $goods */
        $goodsSpu = GoodSpu::whereGoodsId($goodsId)->paginate(30);
        if(count($goodsSpu) > 0){
            foreach($goodsSpu as $goods){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($goods->goods_id)->first();
                if($thisGoods){
                    $goods->goods_name = $thisGoods->name;
                    $goods->thumb_url  = FileManager::getImgUrl($thisGoods->thumb_id);
                }else{
                    $goods->goods_name = null;
                    $goods->thumb_url  = null;
                }
            }
        }

        return view('admin.goods-spu-list',['goodsSpu' => $goodsSpu,'goodsId' => $goodsId,'type' => $type]);
    }

    public function goodsSpuSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id      = !is_null($request->get('id')) ? $request->get('id') : 0;
        $goodsId = !is_null($request->get('goods_id')) ? $request->get('goods_id') : 0;
        $type = 'goods';

        $thisGoodsSpu = GoodSpu::whereId($id)->whereGoodsId($goodsId)->first();

        return  view('admin.goods-spu-set',['thisGoodsSpu' => $thisGoodsSpu,'goodsId' => $goodsId,'type' => $type]);
    }

    public function userIntegralList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var UserIntegralRecord $userInt */
        $userIntegral = UserIntegralRecord::orderBy('created_at','desc')->paginate(30);
        if(count($userIntegral) > 0){
            foreach($userIntegral as $userInt){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($userInt->user_id)->first();
                if($thisUserInfo){
                    $userInt->nickname  = $thisUserInfo->nickname;
                    $userInt->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                }else{
                    $userInt->nickname  = null;
                    $userInt->photo_url = null;
                }

                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($userInt->goods_id)->first();
                if($thisGoods){
                    $userInt->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                }else{
                    $userInt->thumb_url = null;
                }
            }
        }

        return view('admin.user-integral-list',['userIntegral' => $userIntegral]);
    }

    public function qrCodeList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $qrCodes = QrCodes::orderBy('created_at','desc')->paginate(30);

        return view('admin.qrcode-list',['qrCodes' => $qrCodes]);
    }

    public function qrCodSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisQrCode = QrCodes::whereId($id)->first();

        return view('admin.qrcode-set',['thisQrCode' => $thisQrCode]);
    }

    public function expressCompanyList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));
        $express=Express::orderBy('created_at','desc')->paginate(20);

        return view('admin.express-company-list',['express'=>$express]);

    }

    public  function gameRank(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $page = !is_null($request->get("page")) ? $request->get("page") : 1;

        $allUserScores = UserScore::whereNotIn("user_id",[782,761,895,876])->whereBetween("created_at",["2019-05-17 00:00:00","2019-05-28 23:59:59"])->orderBy("score","desc")->paginate(100);

        foreach ($allUserScores as $allUserScore){
            /** @var UserScore $allUserScore */
            $goldNumCount       = UserScoreRecord::whereUserId($allUserScore->user_id)->sum("gold_num");
            $gameScoreCount     = UserScoreRecord::whereUserId($allUserScore->user_id)->whereIn("type",["game","share_relive"])->sum("score");
            $exchangeScoreCount = UserScoreRecord::whereUserId($allUserScore->user_id)->whereType("exchange")->sum("score");

            $allUserScore->gold_num = $goldNumCount;
            $allUserScore->exchange_score = $exchangeScoreCount;
            $allUserScore->score = (int)$gameScoreCount - (int)$exchangeScoreCount;
            $allUserScore->save();

            $thatUser = User::whereId($allUserScore->user_id)->first();
            if($thatUser){
                $thatUserInfo = UserInfo::whereUserId($thatUser->id)->first();

                if($thatUserInfo){
                    $thatUserInfo->photo_url = FileManager::getImgUrl($thatUserInfo->photo_id);
                    $thatUser->user_info = $thatUserInfo;
                }else{
                    $thatUser->user_info = null;
                }

                $allUserScore->user = $thatUser;
            }else{
                $allUserScore->user = null;
            }
        }

        return view('admin.game.game-rank',["allUserScores"=>$allUserScores,"page"=>$page]);

    }

    public function bargainList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var ActivityBargain $activityBargain */
        $activityBargains = ActivityBargain::orderBy('created_at','desc')->paginate(20);
        if(count($activityBargains) > 0){
            foreach($activityBargains as $activityBargain){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereType('bargain')->whereId($activityBargain->goods_id)->first();
                if($thisGoods){
                    $thisGoods->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                    $activityBargain->goods_info = $thisGoods;
                }else{
                    $activityBargain->goods_info = null;
                }

            }
        }

        return view('admin.activity-bargain-list',['activityBargains' => $activityBargains]);
    }

    public function bargainSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var ActivityBargain $thisActivityBargain */
        $thisActivityBargain = ActivityBargain::whereId($id)->first();
        if($thisActivityBargain){
            $thisGoods = Goods::whereId($thisActivityBargain->goods_id)->whereType('bargain')->first();
            if($thisGoods){
                $thisActivityBargain->goods_info = $thisGoods;
            }else{
                $thisActivityBargain->goods_info = null;
            }
        }

        return view('admin.activity-bargain-set',['thisActivityBargain' => $thisActivityBargain]);
    }

    public function userBargainInitiateList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $nickname  = $request->get('nickname');
        $startTime = $request->get('start_time');
        $endTime   = $request->get('end_time');

        /** @var ActivityBargainInitiate $activityBargainInitiate */
        $activityBargainInitiates = ActivityBargainInitiate::orderBy('created_at','desc');

        if(!is_null($nickname) && $nickname != ''){
            $userIds = UserInfo::where('nickname','like','%'.$nickname.'%')->pluck('user_id');

            $activityBargainInitiates->whereIn('user_id',$userIds);
        }

        if(!is_null($startTime) && $startTime != '' && !is_null($endTime) && $endTime != ''){
            $activityBargainInitiates = $activityBargainInitiates->whereBetween('created_at',[$startTime,$endTime]);
        }


        $activityBargainInitiates = $activityBargainInitiates->paginate(20);
        if(count($activityBargainInitiates) > 0){
            foreach($activityBargainInitiates as $activityBargainInitiate){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($activityBargainInitiate->user_id)->first();
                if($thisUserInfo){
                    $activityBargainInitiate->nickname  = $thisUserInfo->nickname;
                    $activityBargainInitiate->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                }else{
                    $activityBargainInitiate->nickname  = null;
                    $activityBargainInitiate->photo_url = null;
                }

                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($activityBargainInitiate->goods_id)->first();
                if($thisGoods){
                    $activityBargainInitiate->goods_name = $thisGoods->name;
                    $activityBargainInitiate->thumb_url  = FileManager::getImgUrl($thisGoods->thumb_id);
                }else{
                    $activityBargainInitiate->goods_name = null;
                    $activityBargainInitiate->thumb_url  = null;
                }
            }
        }

        return view('admin.user-bargain-initiate-list',['activityBargainInitiates' => $activityBargainInitiates,'nickname' => $nickname,'startTime' => $startTime,'endTime' => $endTime]);
    }

    public function userBargainInitiateDetail(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var ActivityBargainInitiate $thisBargainInitiate */
        $thisBargainInitiate = ActivityBargainInitiate::whereId($id)->first();
        if($thisBargainInitiate){
            /** @var UserInfo $thisUserInfo */
            $thisUserInfo = UserInfo::whereUserId($thisBargainInitiate->user_id)->first();
            if($thisUserInfo){
                $thisUserInfo->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                $thisBargainInitiate->user_info = $thisUserInfo;
            }else{
                $thisBargainInitiate->user_info = null;
            }

            /** @var Goods $thisGoods */
            $thisGoods = Goods::whereId($thisBargainInitiate->goods_id)->first();
            if($thisGoods){
                $thisGoods->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                $thisBargainInitiate->goods_info = $thisGoods;
            }else{
                $thisBargainInitiate->goods_info = null;
            }

            $thisGoodsSpu = GoodSpu::whereGoodsId($thisBargainInitiate->goods_id)->whereId($thisBargainInitiate->goods_spu_id)->first();
            if($thisGoodsSpu){
                $thisBargainInitiate->goods_spu_info = $thisGoodsSpu;
            }else{
                $thisBargainInitiate->goods_spu_info = null;
            }

            $thisBargain = ActivityBargain::whereId($thisBargainInitiate->ab_id)->first();
            if($thisBargain){
                $thisBargainInitiate->activity_bargain_info = $thisBargain;
            }else{
                $thisBargainInitiate->activity_bargain_info = null;
            }
        }

        return view('admin.user-bargain-initiate-detail',['thisBargainInitiate' => $thisBargainInitiate]);
    }

    public function userBargainJoinList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var ActivityBargainJoin $activityBargainJoin */
        $activityBargainJoins = ActivityBargainJoin::whereAbiId($id)->orderBy('created_at','desc')->paginate(20);
        if(count($activityBargainJoins) > 0){
            foreach($activityBargainJoins as $activityBargainJoin){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($activityBargainJoin->user_id)->first();
                if($thisUserInfo){
                    $activityBargainJoin->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                    $activityBargainJoin->nickname  = $thisUserInfo->nickname;
                }else{
                    $activityBargainJoin->photo_url = null;
                    $activityBargainJoin->nickname  = null;
                }
            }
        }

        return view('admin.user-bargain-join-list',['activityBargainJoins' => $activityBargainJoins,'abi_id' => $id]);
    }

    public function GroupList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var ActivityGroup $activityGroup */
        $activityGroups = ActivityGroup::orderBy('created_at','desc')->paginate(20);
        if(count($activityGroups) > 0){
            foreach($activityGroups as $activityGroup){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereType('group')->whereId($activityGroup->goods_id)->first();
                if($thisGoods){
                    $thisGoods->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                    $activityGroup->goods_info = $thisGoods;
                }else{
                    $activityGroup->goods_info = null;
                }

            }
        }

        return view("admin.activity-group-list",['activityGroups' => $activityGroups]);
    }

    public function GroupSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var ActivityGroup $thisActivityGroup */
        $thisActivityGroup = ActivityGroup::whereId($id)->first();
        if ($thisActivityGroup) {
            $thisGoods = Goods::whereId($thisActivityGroup->goods_id)->whereType('group')->first();
            if ($thisGoods) {
                $thisActivityGroup->goods_info = $thisGoods;
            } else {
                $thisActivityGroup->goods_info = null;
            }
        }

        return view('admin.activity-group-set',['thisActivityGroup' => $thisActivityGroup]);
    }

    public function groupGoodsSpuList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $goodsId         = !is_null($request->get('goods_id')) ? $request->get('goods_id') : 0;
        $activityGroupId = !is_null($request->get('activity_group_id')) ? $request->get('activity_group_id') : 0;
        $type = 'group';

        /** @var GoodSpu $goods */
        $goodsSpu = GoodSpu::whereGoodsId($goodsId)->paginate(30);
        if(count($goodsSpu) > 0){
            foreach($goodsSpu as $goods){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereId($goods->goods_id)->whereType($type)->first();
                if($thisGoods){
                    $goods->goods_name = $thisGoods->name;
                    $goods->thumb_url  = FileManager::getImgUrl($thisGoods->thumb_id);
                }else{
                    $goods->goods_name = null;
                    $goods->thumb_url  = null;
                }

                /** @var ActivityGroupPrice $activityGroupPrice */
                $activityGroupPrice = ActivityGroupPrice::whereActivityGroupId($activityGroupId)->whereGoodsId($goods->goods_id)->whereGoodsSpuId($goods->id)->first();
                if($activityGroupPrice){
                    $goods->many_price = $activityGroupPrice->many_price;
                }else{
                    $goods->many_price = 0.00;
                }
            }
        }

        return view('admin.goods-spu-list',['goodsSpu' => $goodsSpu,'goodsId' => $goodsId,'type' => $type,'activityGroupId' => $activityGroupId]);
    }

    public function groupGoodsSpuSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id      = !is_null($request->get('id')) ? $request->get('id') : 0;
        $goodsId = !is_null($request->get('goods_id')) ? $request->get('goods_id') : 0;
        $activityGroupId = !is_null($request->get('activity_group_id')) ? $request->get('activity_group_id') : 0;
        $type = 'group';

        $thisGoodsSpu = GoodSpu::whereId($id)->whereGoodsId($goodsId)->first();
        if($thisGoodsSpu){
            /** @var ActivityGroupPrice $activityGroupPrice */
            $activityGroupPrice = ActivityGroupPrice::whereActivityGroupId($activityGroupId)->whereGoodsId($thisGoodsSpu->goods_id)->whereGoodsSpuId($thisGoodsSpu->id)->first();
            if($activityGroupPrice){
                $thisGoodsSpu->many_price = $activityGroupPrice->many_price;
            }else{
                $thisGoodsSpu->many_price = 0.00;
            }
        }

        return  view('admin.goods-spu-set',['thisGoodsSpu' => $thisGoodsSpu,'goodsId' => $goodsId,'type' => $type,'activityGroupId' => $activityGroupId]);
    }

    public function groupAddList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $nickname  = $request->get('nickname');
        $startTime = $request->get('start_time');
        $endTime   = $request->get('end_time');

        /** @var ActivityGroupAdd $groupAdd */
        $groupAddList = ActivityGroupAdd::whereGroupId(0);

        if(!is_null($nickname) && $nickname != ''){
            $userIds = UserInfo::where('nickname','like','%'.$nickname.'%')->pluck('user_id');

            $groupAddList->whereIn('user_id',$userIds);
        }

        if(!is_null($startTime) && $startTime != '' && !is_null($endTime) && $endTime != ''){
            $groupAddList = $groupAddList->whereBetween('created_at',[$startTime,$endTime]);
        }

        $groupAddList = $groupAddList->orderBy('created_at','desc')->paginate(30);
        if(count($groupAddList) > 0){
            foreach($groupAddList as $groupAdd){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($groupAdd->user_id)->first();
                if($thisUserInfo){
                    $groupAdd->nickname  = $thisUserInfo->nickname;
                    $groupAdd->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                }else{
                    $groupAdd->nickname  = null;
                    $groupAdd->photo_url = null;
                }

                /** @var ActivityGroup $thisActivityGroup */
                $thisActivityGroup = ActivityGroup::whereId($groupAdd->ag_id)->first();
                if($thisActivityGroup){
                    /** @var Goods $thisGoods */
                    $thisGoods = Goods::whereId($thisActivityGroup->goods_id)->first();
                    if($thisGoods){
                        $groupAdd->goods_name      = $thisGoods->name;
                        $groupAdd->goods_thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                    }else{
                        $groupAdd->goods_name      = null;
                        $groupAdd->goods_thumb_url = null;
                    }
                }else{
                    $groupAdd->goods_name      = null;
                    $groupAdd->goods_thumb_url = null;
                }
            }
        }

        return view('admin.user-group-add-list',['groupAddList' => $groupAddList,'nickname' => $nickname,'startTime' => $startTime,'endTime' => $endTime]);
    }

    public function groupAddMemberList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $groupId = !is_null($request->get('group_id')) ? $request->get('group_id') : 0;

        /** @var ActivityGroupAdd $groupAddMember */
        $groupAddMemberList = ActivityGroupAdd::whereGroupId($groupId)->orderBy('created_at','desc')->get();
        if(count($groupAddMemberList) > 0){
            foreach($groupAddMemberList as $groupAddMember){
                /** @var UserInfo $thisUserInfo */
                $thisUserInfo = UserInfo::whereUserId($groupAddMember->user_id)->first();
                if($thisUserInfo){
                    $groupAddMember->nickname  = $thisUserInfo->nickname;
                    $groupAddMember->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                }else{
                    $groupAddMember->nickname  = null;
                    $groupAddMember->photo_url = null;
                }

                /** @var ActivityGroup $thisActivityGroup */
                $thisActivityGroup = ActivityGroup::whereId($groupAddMember->ag_id)->first();
                if($thisActivityGroup){
                    /** @var Goods $thisGoods */
                    $thisGoods = Goods::whereId($thisActivityGroup->goods_id)->first();
                    if($thisGoods){
                        $groupAddMember->goods_name      = $thisGoods->name;
                        $groupAddMember->goods_thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);
                    }else{
                        $groupAddMember->goods_name      = null;
                        $groupAddMember->goods_thumb_url = null;
                    }
                }else{
                    $groupAddMember->goods_name      = null;
                    $groupAddMember->goods_thumb_url = null;
                }
            }
        }

        return view('admin.user-group-add-member-list',['groupAddMemberList' => $groupAddMemberList]);
    }
}